package cn.demo.app.cloud;

import cn.demo.app.a.domain.User;
import cn.demo.app.a.service.UserService;
import cn.demo.app.api.EchoApi;
import cn.ifloat.brick.basic.common.exceptions.SystemBasicException;
import cn.ifloat.brick.basic.common.exceptions.SystemExceptionCode;
import cn.ifloat.brick.basic.common.results.FormCondition;
import cn.ifloat.brick.basic.common.results.R;
import cn.ifloat.brick.basic.common.toolkit.FileUtils;
import org.checkerframework.checker.units.qual.A;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author: float 2022/11/2 10:09
 */
@RestController
public class EchoServer implements EchoApi {

    @Autowired
    private UserService userService;

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @PostConstruct
    public void init(){
        System.out.println("init");
    }

    @Override
    public void echo(String msg) {
        logger.info(" echo server :" + msg);
        if ("error".equalsIgnoreCase(msg)) {
            throw new RuntimeException("测试异常 fallBack");
        }
    }

    @Override
    public List<User> list(User user) {
        logger.info("echoserver list");
        return userService.list();
    }

    @Override
    public ResponseEntity<byte[]> download() throws IOException {
        File file = new File("/Users/float/Desktop/demo.sql");
        byte[] bytes = FileUtils.readFileToByteArray(file);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-disposition","attachment;filename=demo.sql");
        ResponseEntity<byte[]> result = new ResponseEntity<>(bytes,headers, HttpStatus.OK);
        return result;
    }

}

// uuid
// 流水号： AB AC123456111111111111111
